package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.ARG_IN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    //todo 分页查询
    @Override
    public PageDTO<BrandDTO> quaryPage(String key, Integer page, Integer rows) {

        //创建分页
        Page<Brand> page1 = new Page<>(page, rows);
        //判断key 输入框是否有值
        boolean noneBlank = StringUtils.isNoneBlank(key);
        System.out.println("11111"+noneBlank+"!!!!!!!!!!!");
        //创建条件构造器
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        //进行条件封装
        queryWrapper.like(noneBlank, "name", key).or()
                .eq(noneBlank, "letter", key);

        //调用page方法
       // Page<Brand> brandPage = page(page1, queryWrapper);
        page(page1, queryWrapper);

       // return new PageDTO<>(brandPage.getTotal(), brandPage.getPages(), BrandDTO.convertEntityList(brandPage.getRecords()));
        return new PageDTO<>(page1.getTotal(), page1.getPages(), BrandDTO.convertEntityList(page1.getRecords()));
    }


    //todo 根据品牌ids 查询品牌
    @Override
    public List<BrandDTO> listBrandByBid(List<Long> ids) {

        List<Brand> brandList = this.listByIds(ids);

        return BrandDTO.convertEntityList(brandList);
    }


    // todo 根据分类id 查询品牌
    @Override
    public List<BrandDTO> queryBrandByCid(Long id) {
        //根据分类查询品牌对象集合 ,一个分类id对应多个品牌id
        List<CategoryBrand> categoryIds = categoryBrandService
                .query()
                .eq("category_id", id)
                .list();
        //根据分类id获得 品牌id的集合 包含分类id

        //创建一个集合,专门放查询出来的 bids
        List<Long> brandIds = new ArrayList<>();

        for (CategoryBrand categoryId : categoryIds) {
            Long brandId = categoryId.getBrandId();
            brandIds.add(brandId);
        }

        //获得根据分类id查询出来的所有的品牌ids,然后在进行查询

        //todo 进行健壮性补充
        if (CollectionUtils.isEmpty(brandIds)){
            throw new LyException(204,"当前分类没有对应的品牌");
           // return null;
        }
        //代码复用
        List<BrandDTO> brandDTOS = listBrandByBid(brandIds);


        return brandDTOS;
    }

    //todo 根据id 查询品牌对象
    @Override
    public BrandDTO queryBrandById(Long id) {

        Brand brand = this.getById(id);
        BrandDTO brandDTO = new BrandDTO(brand);
        return brandDTO;
    }

    //todo:新增品牌
    @Override
    @Transactional
    public void addBrand(BrandDTO brandDTO) {
        //新增是操作数据库表
        Brand brand = brandDTO.toEntity(Brand.class);
        //添加品牌
        this.saveOrUpdate(brand);
         //进行中间表添加
         //前端传来了分类的ids，brand有新增的品牌id
        List<Long> cids = brandDTO.getCategoryIds();
        Long bid = brand.getId();
        for (Long cid : cids) {
            CategoryBrand categoryBrand = CategoryBrand.of(cid, bid);
            //循环遍历cids，并添加进中间表
         categoryBrandService.save(categoryBrand);


        }
    }

    //todo 更新品牌
    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO) {

       /* Brand brand1 = brandDTO.toEntity(Brand.class);
        boolean b = this.updateById(brand1);

        //先删除中间表
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brand1.getId()));
        //然后在添加中间表
        List<Long> cids = brandDTO.getCategoryIds();
        Long bid = brand1.getId();
        for (Long cid : cids) {
            CategoryBrand categoryBrand = CategoryBrand.of(cid, bid);
            //循环遍历cids，并添加进中间表
            categoryBrandService.save(categoryBrand);*/

            //todo 修改品牌之前，先把中间表删除了
            Brand brand = brandDTO.toEntity(Brand.class);
            Long id = brand.getId();
            //根据品牌id 删除中间表
            QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("brand_id", id);

            categoryBrandService.remove(queryWrapper);

            //在继续执行添加
            this.addBrand(brandDTO);

        }



    //todo 删除品牌
    @Override
    @Transactional
    public void deleteBrand(Long id) {
        //删除品牌
        this.removeById(id);

        //删除品牌之后，对中间表进行删除
        QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",id);

        categoryBrandService.remove(queryWrapper);


    }


}